<template>
  <div class="list-table">
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('所在层')" prop="layer">
        <el-input
          v-model="queryParams.layer"
          :placeholder="$t('所在层')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('出租状态')" prop="rentalStatus">
        <el-select v-model="queryParams.rentalStatus" :placeholder="$t('出租状态')" clearable>
          <el-option
            v-for="dict in rentalStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-if="isRoomNumber"
          v-hasPermi="['deal:lease:edit']"
          type="success"
          icon="el-icon-edit"
          size="mini"
          @click="selectEnd"
        >{{ $t('选择完毕') }}</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="dealLicenseDetailsList" border tooltip-effect="darkTable">
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <!--      <el-table-column :label="$t('主表id')" align="center" prop="licenseId" />-->
      <el-table-column :label="$t('楼号')" align="center" prop="floor" width="120" />
      <el-table-column :label="$t('所在层')" align="center" prop="layer" width="120" />
      <el-table-column :label="$t('房号')" align="center" prop="roomNo" width="120" />
      <el-table-column :label="$t('建筑面积')" align="center" prop="constructionArea" width="120" />
      <el-table-column :label="$t('用途')" align="center" prop="use" :formatter="useFormat" width="120" />
      <el-table-column :label="$t('证号')" align="center" prop="certificateNumber" width="270" />
      <el-table-column :label="$t('租约')" align="center" prop="leaseName" width="280">
        <template slot-scope="scope">
          <span>{{ scope.row.leaseName }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('租约到期日期')" align="center" prop="leaseEndDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.leaseEndDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('出租状态')" align="center" prop="rentalStatus" :formatter="rentalStatusFormat" width="120" fixed="right" />
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200" fixed="right">
        <template slot-scope="scope">
          <el-button
            v-if="isRoomNumber && !scope.row.isNoRoomNo"
            size="small"
            type="text"
            icon="el-icon-aim"
            @click="addRoomNo(scope.$index, scope.row)"
          >{{ $t('选择') }}</el-button>
          <el-button
            v-if="isRoomNumber && scope.row.isNoRoomNo"
            size="small"
            type="text"
            icon="el-icon-delete"
            style="color: red"
            @click="deleteRoomNo(scope.$index, scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="$t('租约管理')" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <dealLeaseForm ref="dealLeaseForm" />
    </el-dialog>
  </div>
</template>

<script>
  import { listDealLicenseDetails } from '@/api/deal/license/dealLicenseDetails'
  import dealLeaseForm from '@/views/deal/immovable/dealLeaseForm'
  export default {
    components: {
      dealLeaseForm
    },
    data() {
      return {
        isRoomNumber: false,
        // 遮罩层
        loading: true,
        // 总条数
        total: 0,
        // 物业详情表格数据
        dealLicenseDetailsList: [],
        // 弹出层标题
        title: '物业详情',
        // 是否显示弹出层
        formOpen: false,
        // 用途字典
        useOptions: [],
        // 出租状态字典
        rentalStatusOptions: [],

        // 查询参数
        queryParams: {
          dealId: this.$route.query.id,
          licenseId: undefined,
          layer: undefined,
          rentalStatus: undefined,
          pageNum: 1,
          pageSize: 10
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      }
    },
    created() {
      this.getList()
      this.getDicts('license_details_use').then(response => {
        this.useOptions = response.data
      })
      this.getDicts('license_rental_status').then(response => {
        this.rentalStatusOptions = response.data
      })
    },
    methods: {
      /** 查询物业详情列表 */
      getList(isRoomNumber, empty) {
        // 是否显示选择完毕按钮
        if (isRoomNumber) {
          this.isRoomNumber = isRoomNumber
        }
        if (empty) {
          this.queryParams.rentalStatus = empty
        }
        this.loading = true
        listDealLicenseDetails(this.queryParams).then(response => {
          this.dealLicenseDetailsList = response.rows
          if (this.dealLicenseDetailsList) {
            // 验证房号是否被选中
            this.dealLicenseDetailsList.forEach((item) => {
              item.isNoRoomNo = this.$parent.$parent.form.roomNumbers.some(id => {
                return item.id === id
              })
            })
          }
          this.total = response.total
          this.loading = false
        }).catch(() => {
          this.loading = false
        })
      },
      // 用途字典翻译
      useFormat(row, column) {
        console.log(column)
        return this.selectDictLabel(this.useOptions, row.use)
      },
      // 出租状态字典翻译
      rentalStatusFormat(row, column) {
        console.log(column)
        return this.selectDictLabel(this.rentalStatusOptions, row.rentalStatus)
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          licenseId: undefined,
          floor: undefined,
          layer: undefined,
          roomNo: undefined,
          constructionArea: undefined,
          use: undefined,
          certificateNumber: undefined,
          leaseId: undefined,
          leaseName: undefined,
          leaseEndDate: undefined,
          rentalStatus: undefined,
          remark: undefined,
          createBy: undefined,
          createTime: undefined,
          updateBy: undefined,
          updateTime: undefined,
          delFlag: undefined
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
        // this.queryParams.rentalStatus = 'empty'
      },
      // /** 新增按钮操作 */
      // addHandle() {
      //     this.formOpen = true
      //     this.$nextTick(() => {
      //         this.$refs.dealLicenseDetailsForm.init(null)
      //     })
      // },
      /** 修改按钮操作 */
      updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
          // 调用form页面的 init 方法初始化数据
          this.$refs.dealLeaseForm.init({ id: row.leaseId })
        })
      },
      /** 导出按钮操作 */
      exportHandle() {
        this.downLoadExcel('/license/dealLicenseDetails/export', this.queryParams)
      },
      // 租约管理选择房号
      selectEnd() {
        // 关闭form页面遮罩层
        this.loading = false
        // 关闭form页
        this.$parent.$parent.openDetails = false
      },
      // 添加房号
      addRoomNo(index, row) {
        this.$parent.$parent.form.roomNumbers.push(row.id)
        // 验证房号是否被选中
        // row.isNoRoomNo = true
        // this.dealLicenseDetailsList[index].isNoRoomNo = true
        this.dealLicenseDetailsList[index].isNoRoomNo = true
        const s = JSON.parse(JSON.stringify(this.dealLicenseDetailsList))
        this.dealLicenseDetailsList = s
        if (row.constructionArea) {
          this.$parent.$parent.form.leaseCoveredArea = Number(this.$parent.$parent.form.leaseCoveredArea) + Number(row.constructionArea)
        }
      },
      // 删除房号
      deleteRoomNo(index, row) {
        for (let i = 0; i < this.$parent.$parent.form.roomNumbers.length; i++) {
          if (row.id === this.$parent.$parent.form.roomNumbers[i]) {
            this.$parent.$parent.form.roomNumbers.splice(i, 1)
          }
        }
        this.dealLicenseDetailsList[index].isNoRoomNo = false
        const s = JSON.parse(JSON.stringify(this.dealLicenseDetailsList))
        this.dealLicenseDetailsList = s
        if (row.constructionArea) {
          this.$parent.$parent.form.leaseCoveredArea = Number(this.$parent.$parent.form.leaseCoveredArea) - Number(row.constructionArea)
        }
        // 验证房号是否被选中
      }
    }
  }
</script>
